
/**
	this gramar file is used to parse 'create table' SQLs
	@author wang xudong
	@since 2012-7-30
*/
grammar CreateTable;
@header{
	package wxd.mysqlitemanager.parser.create;
	import wxd.mysqlitemanager.share.db.Column;
	import wxd.mysqlitemanager.share.db.TableInfo;
}

//the start rule,parse sqls and return the TableInfo instance.
start returns [TableInfo info]	
      @init{
      	info = new TableInfo();
      }	
	:
	('create'|'CREATE') ('table'|'TABLE')  '['? ID ']'? '(' columnDef[$info](','columnDef[$info])* ')' (';')? {info.setTableName($ID.text);} 
	;

columnDef[TableInfo table] returns [Column column]
	@init{
		column = new Column();
	}
	:
	ID {column.setColumnName($ID.text);}
	columnType {column.setColumnType($columnType.text);} 
	columnProps[$column]
	{table.addColumn(column);}
	;

columnType
	:'varchar' ('(' NUMBER ')')?
	|'String'
	|'int'
	|'float'
	|'date'
	|'binary'	
	|'long'
	|'text'
	;
columnProps[Column column]
	:
	(a=('not'|'NOT')  ('null'|'NULL'))? {if(a != null) column.setNullable(false);}
	(b=('primary'|'PRIMARY') ('key'|'KEY'))? {if(b != null) column.setPrimaryKey(true); }
	(c=('unique'|'UNIQUE'))? {if(c != null) column.setUnique(true);} 
	;

WS	:(' '|'\r'|'\n'|'\t')+ {skip();};
 
NUMBER	:'1'..'9'('0'..'9')*;
 
ID	:('a'..'z'|'_'|'A'..'Z')('a'..'z'|'_'|'0'..'9'|'A'..'Z')*;

